<template>
    <div class="friend">
        <div class="header">{{ $t('msg.好友') }}</div>
        <div class="content">
            <div class="lemon-sidebar__scroll list">
                <div class="item" v-for="(item, index) in list" :key="index" :class="active == index ? 'active' : ''"
                    @click="change(item, index)">
                    <img :src="item.friend_user_info.avatar" alt="" class="avatar">
                    <div class="name">{{ item.remark || item.friend_user_info.nickname }}</div>
                </div>
            </div>
            <!-- <div class="friend_show" v-if="active > -1">
                <FriendInfo :im_id="im_id" @ok="getList" />
            </div> -->
        </div>
    </div>
</template>

<script>
import { mapState } from 'vuex';
import FriendInfo from "@/components/contacts/FriendInfo.vue"
export default {
    components: {
        FriendInfo
    },
    data() {
        return {
            keywords: "",
            im_id: null,
            active: -1,
            item: null,
            list_arr: [],
            list: []
        }
    },
    computed: {
        ...mapState(['contactKeywords']),
        ...mapState({
            allContacts: state => state.allContacts
        })
    },
    watch: {
        contactKeywords: {
            handler(newVal) {
                this.keywords = newVal;
                this.getList();
            },
            immediate: true
        }
    },
    methods: {
        async getList() {
            this.id = null;
            let { list: res } = await this.$api.chatApi.friendAllList({
                keyword: this.keywords
            });
            this.list = res;
        },
        change(item) {
            const im_id = item.friend_user_info.im_user_id;
            const contact = this.allContacts.find(item => item.user_id == im_id);
            this.$store.commit('changeChatUser', contact);
            setTimeout(() => {
                this.$router.replace({
                    name: 'message'
                })
            }, 100)
        }
        // async change(item, index) {
        //     this.im_id = item.friend_user_info.im_user_id;
        //     this.active = index;
        // }
    }
}
</script>
<style lang="scss" scoped>
.friend {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: #FCFCFE;

    .header {
        height: 92px;
        line-height: 92px;
        font-size: 20px;
        color: #333333;
        padding: 0 24px;
        box-sizing: border-box;
        background: #FCFCFE;
        box-shadow: inset 0px -1px 0px 0px rgba(104, 104, 104, 0.1);
    }

    .content {
        flex: 1;
        display: flex;
        overflow: hidden;

        .list {
            width: 430px;

            cursor: default;
            overflow-y: auto;

            .item {
                height: 88px;
                padding: 10px 24px;
                box-sizing: border-box;
                display: flex;
                align-items: center;

                &.active {
                    background-color: #D9E7FE;
                }

                &:hover:not(&.active) {
                    background: #e3e3e3
                }

                .avatar {
                    width: 52px;
                    height: 52px;
                    border-radius: 4px 4px 4px 4px;
                }

                .avatar {
                    width: 52px;
                    height: 52px;
                    border-radius: 4px 4px 4px 4px;
                }

                .name {
                    flex: 1;
                    padding-left: 12px;
                    box-sizing: border-box;
                    font-size: 18px;
                    color: #333333;

                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                }
            }
        }

        .friend_show {
            flex: 1;
            display: flex;
            justify-content: center;
            align-items: center;
        }
    }
}
</style>